package com.coder4j.javafx;

import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.text.Font;

import java.io.InputStream;

public class MyLabel extends Label {
    public MyLabel(String text) {
        super(text);
    }



    public MyLabel color(String color) {
        this.setStyle(this.getStyle() + "-fx-text-fill:" + color + ";");
        return this;
    }

    /**
     * 设置字体和大小
     * @param size
     * @return
     */
//    public MyLabel fontHuiMo(int size){
//        InputStream fontStream = getClass().getResourceAsStream("/fonts/WenYue-HuiMoShouShu-J.otf");
//        Font customFont = Font.loadFont(fontStream, size);
//        this.setFont(customFont);
//        return this;
//    }

    public MyLabel backgroundImg(String imgUrl) {
        // 设置背景图片
        BackgroundImage backgroundImage = new BackgroundImage(
                ComponentUtils.getImage(imgUrl),
                BackgroundRepeat.NO_REPEAT,
                BackgroundRepeat.NO_REPEAT,
                BackgroundPosition.CENTER,
                new BackgroundSize(1.0, 1.0, true, true, false, false));

        this.setBackground(new Background(backgroundImage));
        return this;
    }

    public MyLabel backgroundColor(String color) {
        this.setStyle(this.getStyle() + "-fx-background-color:" + color + ";");
        return this;
    }

    public MyLabel border(String color, int width, int radius) {
        this.setStyle(this.getStyle() +
                "-fx-border-color: " + color +
                "; -fx-border-width: " + width + "px;");
        if (radius > 0) {
            this.setStyle(this.getStyle() + "-fx-border-radius: " + radius + "px;");
        }
        return this;
    }

    public MyLabel fontSize(int fontSize) {
        this.setStyle(this.getStyle() + "-fx-font-size:" + fontSize + "px;");
        return this;
    }

    public MyLabel padding(int size) {
        this.setStyle(this.getStyle() + "-fx-padding:" + size + "px;");
        return this;
    }

    public MyLabel padding(int v, int h) {
        this.setStyle(this.getStyle() + "-fx-padding:" + v + "px " + h + "px;");
        return this;
    }

    public MyLabel padding(int top, int right, int bottom, int left) {
        this.setStyle(this.getStyle() + "-fx-padding:" + top + "px " + right + "px " + bottom + "px " + right + "px;");
        return this;
    }

    public MyLabel addClickListener(final EventHandler<MouseEvent> eventHandler) {
        // 添加鼠标进入事件监听器
        this.addEventHandler(MouseEvent.MOUSE_ENTERED, event -> {
            // 当鼠标进入ImageView时，改变鼠标形状为小手
            this.setCursor(javafx.scene.Cursor.HAND);
        });

        // 添加鼠标离开事件监听器
        this.addEventHandler(MouseEvent.MOUSE_EXITED, event -> {
            // 当鼠标离开ImageView时，恢复默认鼠标形状
            this.setCursor(javafx.scene.Cursor.DEFAULT);
        });
        this.addEventHandler(MouseEvent.MOUSE_CLICKED, eventHandler);

        return this;
    }



}
